import React, { Component } from "react";

import Childredux from "./childredux";

// import { actionTypes} from '../../redux/action';
import { connect } from "react-redux";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.handleBtnClick = this.handleBtnClick.bind(this);
    this.handleBtnClick1 = this.handleBtnClick1.bind(this);
  }
  // 单一数据
  handleBtnClick() {
    const { dispatch } = this.props;
    const action = {
      // type: actionTypes.ADD_TEST,
      type: "1",
      number: this.props.number,
    };

    dispatch(action); //通过dispatch修改store数据
  }
  handleBtnClick1() {
    this.props.deleteFromArray({ type: "1", number: 12 });
  }

  render() {
    const { PayDecrease ,PayDecrease1} = this.props;
    return (
      <div>
        {/* <button onClick={this.handleBtnClick}>自增</button> */}
        {/* ============================================================== */}
        <button onClick={this.handleBtnClick1}>自增this.handleBtnClick1</button>
        {/* //mapDispatchToProps */}
        <button onClick={PayDecrease}>自增(PayDecrease,mapDispatchToProps)</button>
        <button
          onClick={() => {PayDecrease1(this.props.number) }}
        >
          自增(PayDecrease1,mapDispatchToProps)
        </button>
        {/* //获取store中的数据 */}
        <p>{this.props.number}</p>
        <Childredux></Childredux>
      </div>
    );
  }
}

// 将数据插入 state中 
const mapStateToProps = (state) => {
  return {
    // number: state.number //单个数据
    number: state.a.number, //多个数据
  };
};

//需要触发什么行为(多个数据)
function mapDispatchToProps(dispatch) {
  return {
    deleteFromArray: (array) => {
      console.log(array,'==============   {/* //mapDispatchToProps */}')
      dispatch(array)
    },
    PayDecrease1: (val) =>{
      console.log(val,'============val')
      dispatch({ type: "1", number: val })
    } ,
    PayDecrease: (val) =>{

     return dispatch({ type: "1", number: 10 })
    } ,
  };
}

// export default connect(mapStateToProps)(App);

export default connect(mapStateToProps, mapDispatchToProps)(App); // mapDispatchToProps
